package dreamCookGame;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class AudioClip {
	String _name;
	Clip _soundClip;
	
	public AudioClip(String name) {
		_name = name;
	}

	public void loadResouces(References reference){
		File soundFile = reference.getAudioFile(_name);

		try {
				AudioInputStream stream = AudioSystem.getAudioInputStream(soundFile);
				_soundClip = AudioSystem.getClip();
				_soundClip.open(stream);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}catch (UnsupportedAudioFileException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}catch (LineUnavailableException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	public void play() {
		
		if(!_soundClip.isRunning()){
			_soundClip.start();
		}

	}
	
	public void stop(){
		_soundClip.setFramePosition(0);
		if(_soundClip.isRunning())
			_soundClip.stop();
	}

	public void loop(int numberOfLoops) {
		// TODO Auto-generated method stub
		_soundClip.setFramePosition(0);
		if(numberOfLoops == 0){
			_soundClip.loop(Clip.LOOP_CONTINUOUSLY);
		}else{
			_soundClip.loop(numberOfLoops);
		}

	}
	 
}
